import { createApp } from 'vue';
import App from './App.vue';
import { setupStore, useThemeStore } from './stores';
import { setupRouter } from './router';
import { MONITORING_CONFIG } from '@/config/monitoring';

import {
  setupAntd,
  setupAssets,
  setupGlobalMethods,
  setupGlobalComponents,
} from '@/plugins';

const app = createApp(App);

// 初始化监控
function initMonitoring() {
  // 动态导入监控库
  import('@web-tracing/vue3')
    .then(({ default: WebTracing }) => {
      app.use(WebTracing, MONITORING_CONFIG);
      console.log('[监控] 前端监控系统已启动');
    })
    .catch((error) => {
      console.error('[监控] 初始化失败:', error);
    });
}

function setupPlugins() {
  // 注册全局常用的ant-design-vue组件
  setupAntd(app);
  // 引入静态资源
  setupAssets();
  // 注册全局组件
  setupGlobalComponents(app);
  // 注册全局方法，如：app.config.globalProperties.$message = message
  setupGlobalMethods(app);

  // 初始化监控
  initMonitoring();
}

async function setupApp() {
  // 挂载 pinia 状态管理
  setupStore(app);
  // 注册主题store
  const themeStore = useThemeStore();
  themeStore.initLayoutSetting();

  // 挂载路由
  await setupRouter(app);

  app.mount('#app');
}
setupPlugins();
setupApp();
